package wowraidmanager.server.service.auction;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.slim3.datastore.Datastore;

import wowraidmanager.client.service.auction.AuctionListService;
import wowraidmanager.server.meta.auction.AuctionListMeta;
import wowraidmanager.shared.model.auction.AuctionList;

public class AuctionListServiceImpl implements AuctionListService {

    AuctionListMeta query = AuctionListMeta.get();

    public ArrayList<AuctionList> getAuctionList(Date toDate, Date fromDate){
        return (ArrayList<AuctionList>)Datastore.query(query).filter(query.date.lessThan(toDate), query.date.greaterThan(fromDate)).asList();
    }

    public ArrayList<AuctionList> getAuctionList(Date toDate, Date fromDate, String itemId, String realmName){
        return (ArrayList<AuctionList>)Datastore.query(query).filter(query.date.lessThan(toDate), query.date.greaterThan(fromDate), query.itemId.equal(itemId), query.realm.equal(realmName)).asList();
    }

    public ArrayList<AuctionList> getAuctionList(){
        return (ArrayList<AuctionList>)Datastore.query(query).asList();
    }

    public void setAuctionList(ArrayList<AuctionList> auctionListList){
        Datastore.put(auctionListList);
    }

    public void deleteAllAuctionList(String fromDate, String toDate){
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try{
            Datastore.delete(Datastore.query(query).filter(query.date.greaterThan(DATE_FORMAT.parse(fromDate)), query.date.lessThan(DATE_FORMAT.parse(toDate))).asKeyList());
        }catch(NullPointerException e){
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }catch(IllegalStateException e){
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }catch(ParseException e){
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }
    }
}
